<?php
/**
 * @package    DPCalendar
 * @author     Digital Peak http://www.digital-peak.com
 * @copyright  Copyright (C) 2007 - 2014 Digital Peak. All rights reserved.
 * @license    http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

defined('_JEXEC') or die();

JHtml::addIncludePath(JPATH_COMPONENT . '/helpers/html');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.multiselect');
JHtml::_('dropdown.init');

$user		= JFactory::getUser();
$userId		= $user->get('id');
$listOrder	= $this->escape($this->state->get('list.ordering'));
$listDirn	= $this->escape($this->state->get('list.direction'));
$archived	= $this->state->get('filter.published') == 2 ? true : false;
$trashed	= $this->state->get('filter.published') == -2 ? true : false;
$canOrder	= $user->authorise('core.edit.state', 'com_dpcalendar.category');

JSubMenuHelper::setAction('index.php?option=com_dpcalendar&view=events');

$fLevels	= array();
$fLevels[]	= JHtml::_('select.option', '1', JText::_('J1'));
$fLevels[]	= JHtml::_('select.option', '2', JText::_('J2'));
$fLevels[]	= JHtml::_('select.option', '3', JText::_('J3'));
$fLevels[]	= JHtml::_('select.option', '4', JText::_('J4'));
$fLevels[]	= JHtml::_('select.option', '5', JText::_('J5'));
$fLevels[]	= JHtml::_('select.option', '6', JText::_('J6'));
$fLevels[]	= JHtml::_('select.option', '7', JText::_('J7'));
$fLevels[]	= JHtml::_('select.option', '8', JText::_('J8'));
$fLevels[]	= JHtml::_('select.option', '9', JText::_('J9'));
$fLevels[]	= JHtml::_('select.option', '10', JText::_('J10'));

$eventTypes = array();
$eventTypes[] = JHtml::_('select.option', '1', JText::_('COM_DPCALENDAR_VIEW_EVENTS_SELECT_ORIGIONAL_EVENTS'));
$eventTypes[] = JHtml::_('select.option', '2', JText::_('COM_DPCALENDAR_VIEW_EVENTS_SELECT_BOTH_EVENTS'));

JSubMenuHelper::addFilter(
JText::_('COM_DPCALENDAR_VIEW_EVENTS_SELECT_NORMAL_EVENTS'),
'filter_event_type',
JHtml::_('select.options', $eventTypes, 'value', 'text', $this->state->get('filter.event_type'), true)
);

JSubMenuHelper::addFilter(
JText::_('JOPTION_SELECT_PUBLISHED'),
'filter_published',
JHtml::_('select.options', JHtml::_('jgrid.publishedOptions'), 'value', 'text', $this->state->get('filter.published'), true)
);

JSubMenuHelper::addFilter('- ' .
JText::_('COM_DPCALENDAR_VIEW_EVENTS_SELECT_CALENDAR') . ' -',
'filter_category_id',
JHtml::_('select.options', JHtml::_('category.options', 'com_dpcalendar'), 'value', 'text', $this->state->get('filter.category_id'))
);

JSubMenuHelper::addFilter(
JText::_('JOPTION_SELECT_MAX_LEVELS'),
'filter_level',
JHtml::_('select.options', $fLevels, 'value', 'text', $this->state->get('filter.level'))
);

JSubMenuHelper::addFilter(
JText::_('JOPTION_SELECT_ACCESS'),
'filter_access',
JHtml::_('select.options', JHtml::_('access.assetgroups'), 'value', 'text', $this->state->get('filter.access'))
);

JSubMenuHelper::addFilter(
JText::_('JOPTION_SELECT_AUTHOR'),
'filter_author_id',
JHtml::_('select.options', $this->authors, 'value', 'text', $this->state->get('filter.author_id'))
);

JSubMenuHelper::addFilter(
JText::_('JOPTION_SELECT_LANGUAGE'),
'filter_language',
JHtml::_('select.options', JHtml::_('contentlanguage.existing', true, true), 'value', 'text', $this->state->get('filter.language'))
);

$sortFields = array(
				'a.ordering' => JText::_('JGRID_HEADING_ORDERING'),
				'a.state' => JText::_('JSTATUS'),
				'a.title' => JText::_('JGLOBAL_TITLE'),
				'a.start_date' => JText::_('COM_DPCALENDAR_FIELD_START_DATE_LABEL'),
				'a.access' => JText::_('JGRID_HEADING_ACCESS'),
				'a.hits' => JText::_('JGLOBAL_HITS'),
				'a.language' => JText::_('JGRID_HEADING_LANGUAGE'),
				'a.id' => JText::_('JGRID_HEADING_ID')
);
?>
<script type="text/javascript">
	Joomla.orderTable = function() {
		table = document.getElementById("sortTable");
		direction = document.getElementById("directionTable");
		order = table.options[table.selectedIndex].value;
		if (order != '<?php echo $listOrder; ?>') {
			dirn = 'asc';
		} else {
			dirn = direction.options[direction.selectedIndex].value;
		}
		Joomla.tableOrdering(order, dirn, '');
	}
</script>
<form action="<?php echo JRoute::_('index.php?option=com_dpcalendar&view=events'); ?>" method="post" name="adminForm" id="adminForm">
	<div id="filter-bar" class="btn-toolbar">
		<div class="filter-search btn-group pull-left">
			<label class="element-invisible" for="filter_search"><?php echo JText::_('JSEARCH_FILTER_LABEL'); ?></label>
			<input type="text" name="filter_search" id="filter_search" value="<?php echo $this->escape($this->state->get('filter.search')); ?>"
				title="<?php echo JText::_('COM_DPCALENDAR_SEARCH_IN_TITLE'); ?>" />
		</div>
		<div class="btn-group pull-left">
			<label class="element-invisible" for="filter_search_start"><?php echo JText::_('COM_DPCALENDAR_VIEW_EVENTS_START_DATE_AFTER_LABEL'); ?>:</label>
			<?php echo JHtml::_('calendar',
					$this->escape($this->state->get('filter.search_start')),
					'filter_search_start',
					'filter_search_start',
					'%Y-%m-%d',
					array('class' => 'inputbox', 'maxlength' => '10', 'size' => '10'));?>
		</div>
		<div class="btn-group pull-left">
			<label class="element-invisible" for="filter_search_end"><?php echo JText::_('COM_DPCALENDAR_VIEW_EVENTS_END_DATE_BEFORE_LABEL'); ?>:</label>
			<?php echo JHtml::_('calendar',
					$this->escape($this->state->get('filter.search_end')),
					'filter_search_end',
					'filter_search_end',
					'%Y-%m-%d',
					array('class' => 'inputbox', 'maxlength' => '10', 'size' => '10'));?>
		</div>
		<div class="btn-group pull-left hidden-phone">
			<button class="btn tip" type="submit" rel="tooltip" title="<?php echo JText::_('JSEARCH_FILTER_SUBMIT'); ?>"><i class="icon-search"></i></button>
			<button class="btn tip" type="button"
			onclick="document.id('filter_search').value='';document.id('filter_search_start').value='';document.id('filter_search_end').value='';this.form.submit();"
			rel="tooltip" title="<?php echo JText::_('JSEARCH_FILTER_CLEAR'); ?>"><i class="icon-remove"></i></button>
		</div>
	</div>
	<div class="clearfix"> </div>
	<table class="table table-striped" id="eventList">
		<thead>
			<tr>
				<th width="1%" class="hidden-phone">
					<input type="checkbox" name="checkall-toggle" value="" title="<?php echo JText::_('JGLOBAL_CHECK_ALL'); ?>" onclick="Joomla.checkAll(this)" />
				</th>
				<th width="1%" style="min-width:55px" class="nowrap center">
					<?php echo JHtml::_('grid.sort', 'JSTATUS', 'a.state', $listDirn, $listOrder); ?>
				</th>
				<th class="title">
					<?php echo JHtml::_('grid.sort',  'JGLOBAL_TITLE', 'a.title', $listDirn, $listOrder); ?>
				</th>
				<th width="15%">
					<?php echo JHtml::_('grid.sort',  'JDATE', 'a.start_date', $listDirn, $listOrder); ?>
				</th>
				<th width="3%">
					<?php echo JHtml::_('grid.sort',  'COM_DPCALENDAR_FIELD_COLOR_LABEL', 'a.color', $listDirn, $listOrder); ?>
				</th>
				<th width="5%" class="nowrap hidden-phone">
					<?php echo JHtml::_('grid.sort',  'JGRID_HEADING_ACCESS', 'a.access', $listDirn, $listOrder); ?>
				</th>
				<th width="10%" class="nowrap hidden-phone">
					<?php echo JHtml::_('grid.sort',  'JAUTHOR', 'a.created_by', $listDirn, $listOrder); ?>
				</th>
				<th width="5%" class="nowrap hidden-phone">
					<?php echo JHtml::_('grid.sort', 'JGRID_HEADING_LANGUAGE', 'a.language', $listDirn, $listOrder); ?>
				</th>
				<th width="1%" class="nowrap">
					<?php echo JHtml::_('grid.sort',  'JGRID_HEADING_ID', 'a.id', $listDirn, $listOrder); ?>
				</th>
			</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="11">
					<?php echo $this->pagination->getListFooter(); ?>
				</td>
			</tr>
		</tfoot>
		<tbody>
		<?php foreach ($this->items as $i => $item)
		{
			$canCreate	= $user->authorise('core.create',		'com_dpcalendar.category.' . $item->catid);
			$canEdit	= $user->authorise('core.edit',			'com_dpcalendar.category.' . $item->catid);
			$canCheckin	= $user->authorise('core.manage',		'com_checkin') || $item->checked_out == $user->get('id') || $item->checked_out == 0;
			$canChange	= $user->authorise('core.edit.state',	'com_dpcalendar.category.' . $item->catid) && $canCheckin;
			?>
			<tr class="row<?php echo $i % 2; ?>" sortable-group-id="<?php echo $item->catid?>">
				<td class="center hidden-phone">
					<?php echo JHtml::_('grid.id', $i, $item->id); ?>
				</td>
				<td class="center">
					<div class="btn-group">
						<?php echo JHtml::_('jgrid.published', $item->state, $i, 'events.', $canChange, 'cb', $item->publish_up, $item->publish_down); ?>
						<?php
						$states	= array(
								0	=> array('star-empty',	'events.featured',	'COM_DPCALENDAR_VIEW_EVENTS_UNFEATURED',	'COM_DPCALENDAR_VIEW_EVENTS_TOGGLE_TO_FEATURE'),
								1	=> array('star',		'events.unfeatured',	'COM_DPCALENDAR_FEATURED',		'COM_DPCALENDAR_VIEW_EVENTS_TOGGLE_TO_UNFEATURE'),
						);
						$state	= JArrayHelper::getValue($states, (int) $item->featured, $states[1]);
						$icon	= $state[0];
						if ($canChange)
						{
							echo '<a href="#" onclick="return listItemTask(\'cb' . $i . '\',\'' . $state[1] . '\')" class="btn btn-micro ' .
								($item->featured == 1 ? 'active' : '') . '"
								rel="tooltip" title="' . JText::_($state[3]) . '"><i class="icon-'
								. $icon . '"></i></a>';
						}
						?>
					</div>
				</td>
				<td class="nowrap has-context">
					<div class="pull-left">
						<?php if ($item->checked_out)
						{ ?>
							<?php echo JHtml::_('jgrid.checkedout', $i, $item->editor, $item->checked_out_time, 'events.', $canCheckin); ?>
						<?php
						} ?>
						<?php if ($item->language == '*')
						{?>
							<?php $language = JText::alt('JALL', 'language'); ?>
						<?php
						} else
						{?>
							<?php $language = $item->language_title ? $this->escape($item->language_title) : JText::_('JUNDEFINED'); ?>
						<?php
						}?>
						<?php if ($canEdit || $canEditOwn)
						{ ?>
							<a href="<?php echo JRoute::_('index.php?option=com_dpcalendar&task=event.edit&id=' . $item->id);?>"
								title="<?php echo JText::_('JACTION_EDIT');?>">
								<?php echo $this->escape($item->title); ?></a>
						<?php
						} else
						{ ?>
							<span title="<?php echo JText::sprintf('JFIELD_ALIAS_LABEL', $this->escape($item->alias));?>">
								<?php echo $this->escape($item->title); ?>
							</span>
						<?php
						} ?>
						<div class="small">
							<?php echo JText::_('COM_DPCALENDAR_CALENDAR') . ": " . $this->escape($item->category_title); ?>
						</div>
					</div>
					<div class="pull-left">
						<?php
							// Create dropdown items
							JHtml::_('dropdown.edit', $item->id, 'event.');

							if ($item->original_id > 0)
							{
								JHtml::_('dropdown.addCustomItem',
								JText::_('COM_DPCALENDAR_VIEW_EVENTS_DROPDOWN_EDIT_ORIGINAL'),
								JRoute::_('index.php?option=com_dpcalendar&task=event.edit&id=' . $item->original_id));
							}

							JHtml::_('dropdown.divider');
							if ($item->state)
							{
								JHtml::_('dropdown.unpublish', 'cb' . $i, 'events.');
							}
							else
							{
								JHtml::_('dropdown.publish', 'cb' . $i, 'events.');
							}

							if ($item->featured)
							{
								JHtml::_('dropdown.unfeatured', 'cb' . $i, 'events.');
							}
							else
							{
								JHtml::_('dropdown.featured', 'cb' . $i, 'events.');
							}

							JHtml::_('dropdown.divider');

							if ($archived)
							{
								JHtml::_('dropdown.unarchive', 'cb' . $i, 'events.');
							}
							else
							{
								JHtml::_('dropdown.archive', 'cb' . $i, 'events.');
							}

							if ($item->checked_out)
							{
								JHtml::_('dropdown.checkin', 'cb' . $i, 'events.');
							}

							if ($trashed)
							{
								JHtml::_('dropdown.untrash', 'cb' . $i, 'events.');
							}
							else
							{
								JHtml::_('dropdown.trash', 'cb' . $i, 'events.');
							}

							// Render dropdown list
							echo JHtml::_('dropdown.render');
							?>
					</div>
				</td>
				<td class="small hidden-phone">
					<?php echo DPCalendarHelper::renderEvents(array($item), '{{#events}}{{date}}{{/events}}'); ?>
				</td>
				<td class="small hidden-phone" style="background: none repeat scroll 0 0 #<?php echo $item->color;?>">
				</td>
				<td class="small hidden-phone">
					<?php echo $this->escape($item->access_level); ?>
				</td>
				<td class="small hidden-phone">
					<?php echo $this->escape($item->author_name); ?>
				</td>
				<td class="small hidden-phone">
					<?php if ($item->language == '*')
					{?>
						<?php echo JText::alt('JALL', 'language'); ?>
					<?php
					}
					else
					{?>
						<?php echo $item->language_title ? $this->escape($item->language_title) : JText::_('JUNDEFINED'); ?>
					<?php
					}?>
				</td>
				<td class="center hidden-phone">
					<?php echo (int) $item->id; ?>
				</td>
			</tr>
			<?php
			} ?>
		</tbody>
	</table>

	<?php echo $this->loadTemplate('batch'); ?>

		<input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="filter_order" value="<?php echo $listOrder; ?>" />
		<input type="hidden" name="filter_order_Dir" value="<?php echo $listDirn; ?>" />
		<?php echo JHtml::_('form.token'); ?>
</form>

<div align="center" style="clear: both">
	<?php echo sprintf(JText::_('COM_DPCALENDAR_FOOTER'), JRequest::getVar('DPCALENDAR_VERSION'));?>
</div>
